#ifndef __WOBJECT_CPP__
#define __WOBJECT_CPP__

#include "WExt/whead.h"

objectdata* objectlist;
pthread_mutex_t pfreeobject;
int curobject;

CObject::CObject()
{
	classname = "CObject";
	pos = _CPoint(0, 0);
}

CObject::~CObject()
{
}

int getnewobjectid()
{
	pthread_mutex_lock(&pfreeobject);
	for (int i = curobject;i < maxobject;i++) {
		if (objectlist[i].objectpoint == NULL) {
			pthread_mutex_unlock(&pfreeobject);
			curobject = i+1;
			return i;
		}
	}
	for (int i = 1;i < curobject;i++) {
		if (objectlist[i].objectpoint == NULL) {
			pthread_mutex_unlock(&pfreeobject);
			curobject = i+1;
			return i;
		}
	}
	return -1;
	pthread_mutex_unlock(&pfreeobject);
}

CObject* getObjectByObjectID(int objectid)
{
	if (objectid <= 0 || objectid >= maxobject) return NULL;
	CObject* object = (CObject*)objectlist[objectid].objectpoint;
	if (object->classname != "CObject") return NULL;
	return object;
}

int createObject()
{
	int objectid = getnewobjectid();
	if (objectid == -1) return -1;
	CObject* object = new CObject;
	object->objectid = objectid;
	objectlist[objectid].objectpoint = object;
	wserPrintMsg("create object. objectid:%d\n", objectid);
	return objectid;
}

void removeObject(int objectid)
{
	CObject* object = getObjectByObjectID(objectid);
	if (object == 0) return;
	wserPrintMsg("remove object. objectid:%d\n", object->objectid);
	objectlist[object->objectid].objectpoint = NULL;
	delete object;
}

#endif //__WOBJECT_CPP__
